package com.example.snakefinal;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Arrays;
import java.util.List;

public class SnakeSkinChoice extends AppCompatActivity {

    private RecyclerView recyclerView;
    private SnakeSkinImageAdapter imageAdapter;
    private ImageView leftImageView;  // Used to display the selected skin

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.skin_choice);

        // Get the ImageView on the left
        leftImageView = findViewById(R.id.leftImageView);

        // Load the last selected skin resource ID from the SharedPreferences
        SharedPreferences sharedPref = getSharedPreferences("SnakeGamePrefs", Context.MODE_PRIVATE);
        int savedSkinResource = sharedPref.getInt("selected_skin", R.drawable.body3);
        drawSnakePreview(savedSkinResource); // Draw the saved skin

        // Get the RecyclerView
        recyclerView = findViewById(R.id.recyclerView);

        // Create a LinearLayoutManager to arrange the images vertically
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        // Prepare a list of image asset ids
        List<Integer> imageList = Arrays.asList(
                R.drawable.body3,
                R.drawable.body4,
                R.drawable.body5,
                R.drawable.body7,
                R.drawable.body8,
                R.drawable.body9,
                R.drawable.body10,
                R.drawable.body11
                // add more images here
        );

        // Returns the click event for the button
        Button btnBack = findViewById(R.id.btnBack);
        btnBack.setOnClickListener(v -> finish());  // End the current activity and return to the previous page

        // Create the adapter and set up the click event
        imageAdapter = new SnakeSkinImageAdapter(this, imageList, imageResId -> {
            // When the player selects skin, draw snake preview
            drawSnakePreview(imageResId);

            // Use SharedPreferences to save the selected skin resource ID
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putInt("selected_skin", imageResId);
            editor.apply();
        });
        recyclerView.setAdapter(imageAdapter);
    }

    // Draw a preview of the snake and display it in the left ImageView
    private void drawSnakePreview(int bodyResourceId) {
        // Set the length of the snake and the size of each section
        int snakeLength = 5; // length
        int segmentSize = 50; // size
        int overlap = segmentSize / 2; // 50% overlap
        int snakeWidth = segmentSize + (snakeLength - 1) * overlap; // whole length
        int snakeHeight = segmentSize;
        int offsetX = -20; // move left to adjust the position

        // Create the bitmap and canvas to draw the snake
        Bitmap snakeBitmap = Bitmap.createBitmap(snakeWidth, snakeHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(snakeBitmap);
        Paint paint = new Paint();

        // Get a picture of the snake's head and body
        Bitmap headBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.head);
        headBitmap = Bitmap.createScaledBitmap(headBitmap, segmentSize, segmentSize, false);
        Bitmap bodyBitmap = BitmapFactory.decodeResource(getResources(), bodyResourceId);
        bodyBitmap = Bitmap.createScaledBitmap(bodyBitmap, segmentSize, segmentSize, false);

        // Draw the snake body segments, from left to right, with 50% overlap in each segment
        for (int i = 0; i < snakeLength; i++) {
            int xPosition = offsetX + i * overlap; // Draw the snake body segments, from left to right, with each segment resetting the horizontal position of each segment, including 50% overlap and offset
            canvas.drawBitmap(bodyBitmap, xPosition, 0, paint);
        }

        // Draw the snake's head on the far right side of the body
        canvas.drawBitmap(headBitmap, offsetX + snakeLength * overlap, 0, paint);

        // Set the drawn snake bitmap to the content of the ImageView on the left
        leftImageView.setImageBitmap(snakeBitmap);
    }

}
